$(function(){
	alert(token("香港运动购"));
	var cnc_account = $.cookie("cnc_account");
	var cnc_last_account = $.cookie("cnc_last_account");
	
	var userids = [];
	if(cnc_account) {
		cnc_account = eval("("+cnc_account+")");
		for(var key in cnc_account){
			userids.push(key);
		}
	}else{
		cnc_account = {};
	}
	
	if(cnc_last_account){
		cnc_last_account = eval("("+cnc_last_account+")");
		$("#userid").val(cnc_last_account.userid);
		$("#mobilenum").val(cnc_last_account.value);
		if(cnc_last_account.token && cnc_last_account.autoLogin){
			window.location.href = 'index.html';
		}
	}
	
	$("#userid").catcomplete({
		appendTo: "#addConfForm",
		delay: 0,
		source: userids,
		category:false,
		renderItem:function(ul, item) {
			return $("<li><a>"+item.value+"</a></li>").appendTo(ul);
		}
	}).bind("keyup keypress", function(){
		var users = cnc_account[this.value];
		if(!users || !users[$("#mobilenum").val()]){
			$("#mobilenum").val("");
		}
	});
	
	$("#mobilenum").catcomplete({
		appendTo: "#addConfForm",
		delay: 0,
		source: function(request, response){
			var users = cnc_account[$("#userid").val()];
			var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i");
			var source = [];
			$.each(users?users:[], function(key, item) {
				if(matcher.test(key))
					source.push(item);
			});
	        response(source);
	    },
		category:false,
		renderItem:function(ul, item) {
			return $("<li><a>"+item.value+"</a></li>").appendTo(ul);
		}
	});
	$("#login").click(function(){
		var mobilenum = $("#mobilenum").val();
		var password = $("#password").val();
		ajax("login",{
			data:{
				login_name:mobilenum,
				login_pwd:password
			},
			success:function(dataResult){
				if(dataResult && dataResult.result == 0){
					
				}else{
					$("#loginDiv").proAlert({
						alert:"danger",
						msg:dataResult.msg, 
						delay:2000,
						style:"position: absolute;z-index:9999999"
					});
				}
			}
		});
	});
	/*$("#login").click(function(){
		if(!validate("userid,mobilenum,password")){
			return;
		}
		var userid = $("#userid").val();
		var mobilenum = $("#mobilenum").val();
		var password = $("#password").val();
		ajax("login",{
			safe:true,
			data:{
				userid:userid,
				mobilenum:mobilenum,
				password:password
			},
			success:function(dataResult){
				if(dataResult && dataResult.executeResult && dataResult.executeResult.result == 1){
					var user = dataResult.dataView.synergyLoginUser[0];
					
					if(!cnc_account[userid]){
						cnc_account[userid] = {};
					}
					var account = {
						userid:userid,
						value:mobilenum
					};
					cnc_account[userid][mobilenum] = account;
					$.cookie("cnc_account", $.json2Str(cnc_account), { expires: 7 });
					account.token = user.accessToken;
					account.autoLogin = $("#autoLogin")[0].checked;
					$.cookie("cnc_last_account", $.json2Str(account), { expires: 7 });
					
					$("#loginDiv").proAlert({
						alert:"info",
						msg:"登录成功", 
						delay:2000,
						style:"position: absolute;z-index:9999999"
					});
					window.location.href = 'index.html';
				}else{
					$("#loginDiv").proAlert({
						alert:"danger",
						msg:dataResult.executeResult.errorDescr, 
						delay:2000,
						style:"position: absolute;z-index:9999999"
					});
				}
			}
		});
	});*/
	
	$(document).keyup(function(event){
		if(event.keyCode == 13){
			$("#login").trigger("click");
		}
	});
	
	$("#getIdentiCode").click(function(){
		var reuserid = $("#re_userid").val();
		var remobilenum = $("#re_mobilenum").val();
		if(!validate("re_userid,re_mobilenum")){
			return;
		}
		 ajax("sendSMS",{
			data:{
				userid:reuserid,
				mobilenum:remobilenum,
				type:2
			},
			success:function(dataResult){
				$("#resetPwdDiv").proAlert({
					alert:dataResult.executeResult.result?"success":"danger",
					msg:dataResult.executeResult.errorDescr,
					delay:2000,
					style:"position: absolute;z-index:9999999"
				});    
		   }
		});
	});
	
	$("#restSuccess").click(function(){ 
		var reuserid=$("#re_userid").val();
		var remobilenum=$("#re_mobilenum").val();
		var validcode=$("#validCode").val();
		var newpassword=$("#new_password").val();	
		if(!validate("re_userid,re_mobilenum,new_password")){
			return;
		}
		ajax("modifyPassword",{
			data:{
				userid:reuserid,
				mobilenum:remobilenum,
				code:validcode,
				oldpassword:"",
				newpassword:newpassword
			},
			success:function(dataResult){
				$("#resetPwdDiv").proAlert({
					alert:dataResult.executeResult.result?"success":"danger",
					msg:dataResult.executeResult.errorDescr,
					delay:2000,
					style:"position: absolute;z-index:9999999"
				});
			}
		});
	});
	
	$("#userid,#mobilenum,#password,#re_userid,#re_mobilenum,#new_password").bind("blur keyup keypress",function(){
		validate(this.id);
	});
	
	$("#rep_password").bind("blur keyup keypress",function(){
		var new_password=$("#new_password").val();
		var rep_password=this.value;
		if(!(new_password==rep_password&&rep_password!='')){
			$(this).parent().addClass("has-error");
			$("#rep_passworderror").show();
		}else{
			$(this).parent().removeClass("has-error");
			$("#rep_passworderror").hide();
		}
	});
	isBrowser();
});
var regRule = {
	userid:["/^[0-9]{1,6}$/","errUserId"],
	mobilenum:["/^0?(13[0-9]|15[012356789]|18[012356789]|14[57])[0-9]{8}$/","mobilenumerror"],
	password:["/^[a-zA-Z0-9_]{6,20}$/","passworderror"]
};

function validate(idStr){
	var ids = idStr.split(",");
	var globPass = true;
	for(var key in ids){
		var index = ids[key];
		var id = "#"+index;
		var reg = eval($(id).attr("data-reg")?$(id).attr("data-reg"):regRule[index][0]);
		var target = "#"+($(id).attr("data-target")?$(id).attr("data-target"):regRule[index][1]);
		var value = $(id).val();
		var pass = reg.test(value);
		if(!pass){
			$(id).parent().addClass("has-error");
			$(target).show();
			if(globPass)
				globPass = false;
		}else{
			$(id).parent().removeClass("has-error");
			$(target).hide();
		}
	}
	return globPass;
}

function isBrowser(){
	    var Sys = {};
	    var ua = navigator.userAgent.toLowerCase();
	    var s;
	    (s = ua.match(/msie ([\d.]+)/))?Sys.ie = s[1]:
	    (s = ua.match(/firefox\/([\d.]+)/))?Sys.firefox = s[1]:
	    (s = ua.match(/chrome\/([\d.]+)/))?Sys.chrome = s[1]:
	    (s = ua.match(/opera.([\d.]+)/))?Sys.opera = s[1]:
	    (s = ua.match(/version\/([\d.]+).*safari/))?Sys.safari = s[1]:0;
	    if(Sys.ie){
	        if(Sys.ie == '6.0'||Sys.ie =='7.0'){
	        	alert("您所用的版本不兼容，推荐您使用Chrome,Firefox,Opera,Safari,IE8及以上");
	        	window.close();
	        }
	    }
	}
